/**
 * Data processing module for the sample TypeScript application.
 */

import { readFile } from 'fs/promises';
import { readFileSafely } from '../utils/helpers';

export interface ProcessResult {
  length: number;
  lines: number;
  words: number;
  analysisComplete: boolean;
}

export class DataProcessor {
  private processedCount: number = 0;
  private errors: string[] = [];

  constructor() {
    // Initialize processor
  }

  async processFile(filePath: string): Promise<ProcessResult> {
    try {
      const content = await readFileSafely(filePath);
      const result = this.analyzeContent(content);
      this.processedCount++;
      return result;
    } catch (error) {
      const errorMsg = error instanceof Error ? error.message : String(error);
      this.errors.push(errorMsg);
      throw error;
    }
  }

  private analyzeContent(content: string): ProcessResult {
    return {
      length: content.length,
      lines: content.split('\n').length,
      words: content.split(/\s+/).filter(word => word.length > 0).length,
      analysisComplete: true
    };
  }

  async saveResult(result: ProcessResult, outputPath: string): Promise<void> {
    const fs = await import('fs/promises');
    await fs.writeFile(outputPath, JSON.stringify(result, null, 2));
  }

  async batchProcess(filePaths: string[]): Promise<ProcessResult[]> {
    const results: ProcessResult[] = [];
    
    for (const path of filePaths) {
      try {
        const result = await this.processFile(path);
        results.push(result);
      } catch (error) {
        // Continue processing other files
        continue;
      }
    }
    
    return results;
  }

  getStatistics(): { processedCount: number; errorCount: number } {
    return {
      processedCount: this.processedCount,
      errorCount: this.errors.length
    };
  }
}